package com.xpec.canaan.util;

import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;

/**
 * Process all files in this directory, without files inside sub directory
 * @author Wilson Shih
 *
 */
public class ProcessSingleDirectoryRecorder {

	public ProcessSingleDirectoryRecorder()
	{
		m_elementRecorder = new TableElementRecorder();	
	}
	
	public static boolean preProcess(String dirPath, ArrayList<File> baseFiles, ArrayList<File> packageDirs)
	{
		File baseDir = new File(dirPath);		
		
		if( null == baseFiles || null == packageDirs )
		{
			return false;
		}
		
		baseFiles.clear();
		packageDirs.clear();
		
		File[] allFiles = baseDir.listFiles();
		File testF;
		for( int i = 0; i < allFiles.length; i++ )
		{
			testF = allFiles[i];
			if( testF.isFile() )
			{
				baseFiles.add(testF);
			}
			else if( testF.isDirectory() )
			{
				packageDirs.add(testF);
			}
		}
		
		for( int i = 0; i < baseFiles.size(); i++ )
		{
			testF = baseFiles.get(i);
		}
		
		return true;
	}
	
	public void processFilesMD5(ArrayList<File> procFiles, OutputMD5Process mainProc, ExecutorService threadPool, String writePath)
	{
		if( null == procFiles || 0 == procFiles.size() )
		{
			System.out.println("no files");
			return;
		}
		
		if( null == writePath || "" == writePath)
			return;
		
		m_fileRecorder = new FileOutputRecorder(writePath);
		if( !m_fileRecorder.createOutFile() )
		{
			return;
		}
		
		m_mainProcess = mainProc;
		
		m_totalCount = procFiles.size();
		m_completeCount = 0;
		
		for( int i = 0; i < procFiles.size(); i++ )
		{
			File f = procFiles.get(i);
			if( f.exists() )
			{
				if( !f.isDirectory() )
				{
					ProduceMD5 tep = new ProduceMD5(f,m_elementRecorder, this);
					threadPool.execute(tep);
				}
			}
		}
	}
	
	public void processFilesMD5(File[] procFiles, OutputMD5Process mainProc, ExecutorService threadPool, String writePath)
	{
		if( null == procFiles || 0 == procFiles.length )
		{
			System.out.println("no files");
			return;
		}
		
		if( null == writePath || "" == writePath)
			return;
		
		m_fileRecorder = new FileOutputRecorder(writePath);
		if( !m_fileRecorder.createOutFile() )
		{
			return;
		}
		
		m_mainProcess = mainProc;
		
		m_totalCount = procFiles.length;
		m_completeCount = 0;
		
		for( int i = 0; i < procFiles.length; i++ )
		{
			File f = procFiles[i];
			if( f.exists() )
			{
				if( !f.isDirectory() )
				{
					ProduceMD5 tep = new ProduceMD5(f,m_elementRecorder, this);
					threadPool.execute(tep);
				}
			}
		}
	}
	public synchronized void notifyFileFinished()
	{
		m_completeCount++;
		if( m_totalCount == m_completeCount )
		{
			// process end
			// output result
			m_elementRecorder.dumpToFile(m_fileRecorder);
			// tell main finished
			m_mainProcess.notifySingleDirComplete();
		}
	}
	
	private FileOutputRecorder m_fileRecorder;
	private TableElementRecorder m_elementRecorder;
	
	private int m_completeCount;
	private int m_totalCount;
	
	private OutputMD5Process m_mainProcess;
	
}
